﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace HFUTCourseSimulation.Util.WpfValueConv {

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class EnableToVisibility : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            var v = (bool)value;
            return v ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(Kernel.ReporterKind), typeof(Color))]
    public class MsgKindToForegroundColor : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            var v = (Kernel.ReporterKind)value;
            switch (v) {
                case Kernel.ReporterKind.Info:
                    return ColorConsistency.InfoColor.Foreground;
                case Kernel.ReporterKind.Warning:
                    return ColorConsistency.WarningColor.Foreground;
                case Kernel.ReporterKind.Error:
                    return ColorConsistency.ErrorColor.Foreground;
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(Kernel.ReporterKind), typeof(Color))]
    public class MsgKindToBackgroundColor : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            var v = (Kernel.ReporterKind)value;
            switch (v) {
                case Kernel.ReporterKind.Info:
                    return ColorConsistency.InfoColor.Background;
                case Kernel.ReporterKind.Warning:
                    return ColorConsistency.WarningColor.Background;
                case Kernel.ReporterKind.Error:
                    return ColorConsistency.ErrorColor.Background;
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(Kernel.ReporterKind), typeof(BitmapSource))]
    public class MsgKindToIcon : IValueConverter {
        private static BitmapSource CreateWpfSystemIcon(System.Drawing.Icon icon) {
            return Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }
        private static readonly BitmapSource InfoIcon = CreateWpfSystemIcon(System.Drawing.SystemIcons.Information);
        private static readonly BitmapSource WarningIcon = CreateWpfSystemIcon(System.Drawing.SystemIcons.Warning);
        private static readonly BitmapSource ErrorIcon = CreateWpfSystemIcon(System.Drawing.SystemIcons.Error);

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            var v = (Kernel.ReporterKind)value;
            switch (v) {
                case Kernel.ReporterKind.Info:
                    return InfoIcon;
                case Kernel.ReporterKind.Warning:
                    return WarningIcon;
                case Kernel.ReporterKind.Error:
                    return ErrorIcon;
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

}
